package com.cykj.controller;

import com.cykj.annoation.Log;
import com.cykj.dto.ResponseDTO;
import com.cykj.entity.CourseDO;
import com.cykj.service.ICourseService;
import com.cykj.util.FileUploadUtil;
import com.cykj.vo.CourseVO;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;

/**
 * @Program: Mooc-Admin
 * @Package: com.cykj.controller
 * @Author: 张俊杰
 * @Create: 2025-07-09 11:25
 * @Description: TODO
 * @Vision: 1.0
 **/
@RestController
@RequestMapping("course")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @Operation(summary = "获取所有课程")
    @Log(model = "课程模块", value = "获取所有课程")
    @GetMapping("list")
    public ResponseDTO getListCourse(CourseVO courseVO) {
        return courseService.getListCourse(courseVO);
    }

    @Operation(summary = "删除课程")
    @Log(model = "课程模块", value = "删除课程")
    @DeleteMapping("delete/{courseId}")
    public ResponseDTO deleteCourse(@PathVariable Integer courseId) {
        return courseService.deleteCourse(courseId);
    }

    /**
     * @Description:添加课程
     * @Param:[courseName, courseIntroduce, coursePrice, courseCover, courseVideo]
     * @Return:com.cykj.dto.ResponseDTO
     */
    @Operation(summary = "添加课程")
    @Log(model = "课程模块", value = "添加课程")
    @PostMapping("add")
    public ResponseDTO addCourse(@RequestParam("courseName") String courseName,
                                 @RequestParam("courseIntroduce") String courseIntroduce,
                                 @RequestParam("coursePrice") Double coursePrice,
                                 @RequestParam("categoryId") Integer categoryId,
                                 @RequestParam("courseCover") MultipartFile courseCover,
                                 @RequestParam("courseVideo") MultipartFile courseVideo) {

        String s = fileUploadUtil.uploadFile(courseCover);
        String s1 = fileUploadUtil.uploadFile(courseVideo);
        CourseDO courseDO = new CourseDO(courseName,categoryId, courseIntroduce, s, s1, coursePrice);
        return courseService.addCourse(courseDO);
    }

    /**
     * @Description:修改课程
     * @Param:[courseId, courseName, courseIntroduce, coursePrice, courseCover, courseVideo]
     * @Return:com.cykj.dto.ResponseDTO
     */
    @Operation(summary = "修改课程")
    @Log(model = "课程模块", value = "修改课程")
    @PutMapping("update")
    public ResponseDTO updateCourse(@RequestParam("courseId") Integer courseId,
                                    @RequestParam("courseName") String courseName,
                                    @RequestParam("courseIntroduce") String courseIntroduce,
                                    @RequestParam("coursePrice") Double coursePrice,
                                    @RequestParam("categoryId") Integer categoryId,
                                    @RequestParam(value = "courseCover", required = false) MultipartFile courseCover,
                                    @RequestParam(value = "courseVideo", required = false) MultipartFile courseVideo) {


        String s = null;
        if (courseCover != null) {
            s = fileUploadUtil.uploadFile(courseCover);
        }
        String s1 = null;
        if (courseVideo != null) {
            s1 = fileUploadUtil.uploadFile(courseVideo);
        }

        CourseDO courseDO = new CourseDO(courseId, courseName, courseIntroduce, categoryId, s, s1, coursePrice);
        return courseService.updateCourse(courseDO);
    }
}
